<template>
  <waterfall :col="col" :data="data">

    <div class="cell-item" v-for="(item, index) in data" :key="index">
      <img :src="item.imgSrc" alt="" @click="showDetail(item)" />
      <div class="item-body">
        <div class="item-desc" @click="editName(item)">{{ item.title }}</div>

      </div>
    </div>

    <!-- 弹窗 -->
    <el-dialog :title="imgDetail" :visible.sync="dialogVisible2" :before-close="handleClose"
      :modal-append-to-body="false" width="800px">
      <img :src="this.imgDetailSrc" alt="" class="animation" />
    </el-dialog>

    <el-dialog :title="editTitle" :visible.sync="dialogVisible" width="40%" :before-close="handleClose">
      <el-form :model="ruleForm" label-width="100px">
        <el-form-item label="原名称：" prop="oldName">
          <el-input v-model="ruleForm.oldName"></el-input>
        </el-form-item>
        <el-form-item label="新名称：" prop="newName">
          <el-input v-model="ruleForm.newName"></el-input>
        </el-form-item>
      </el-form>
      <el-button type="primary" @click="dialogVisibleConfirm">确 定</el-button>
    </el-dialog>

  </waterfall>
</template>
<script>
import axios from 'axios'

export default {
  name: 'Waterfal',
  props: {
    data: {
      type: Array,
      default: () => [],
    },
  },
  data() {
    return {
      col: 4,
      dialogVisible2: false,
      dialogVisible: false,
      imgDetailSrc: '',
      imgDetail: '图片详情',
      editTitle: '更改名称',
      ruleForm: {
        oldName: '',
        newName: '',
        imgPath: '',
      },
    }
  },
  methods: {
    showDetail(item) {
      var that = this
      this.imgDetailSrc = item.imgSrc
      this.dialogVisible2 = true
      console.log('imgDetailSrc' + this.imgDetailSrc)
    },
    editName(item) {
      var that = this
      this.dialogVisible = true
      that.ruleForm.oldName = item.title
      that.ruleForm.imgPath = item.fullSrc
      console.log(item)
    },
    handleClose(done) {
      this.dialogVisible2 = false
      this.dialogVisible = false
    },
    dialogVisibleConfirm() {
      var that = this
      console.log(that.ruleForm.oldName)
      console.log(that.ruleForm.imgPath)
      axios({
        method: 'get',
        url:
          'https://localhost:44313/Album/EditTitle?newName=' +
          that.ruleForm.newName +
          '&title=' +
          that.ruleForm.oldName +
          '&path=' +
          that.ruleForm.imgPath,
      }).then(
        (res) => {
          if (res.data === -1) {
            that.$message.error('该目录下已存在相同的名字！')
          }
          if (res.data === 500) {
            that.$message.error('找不到原文件！')
          }
          if (res.data === 200) {
            that.$message.success('更改成功')
          }
          console.log(res)
        },
        (res) => {
          console.log(res)
          that.$message.error('更改失败')
        }
      )
      that.dialogVisible = false
    },
  },
}
</script>
<style lang="scss" scoped>
.el-dialog {
  .animation {
    width: 80%;
    height: 80%;
    margin-left: 10%;
    img {
      vertical-align: middle;
      text-align: center;
    }
  }
}
.item-body {
  margin: 9px;
  .item-desc {
    text-align: left;
    font-family: Roboto;
    font-style: normal;
    font-weight: normal;
    font-size: 14px;
    line-height: 16px;
    color: #000000;
  }
  .item-footer {
    display: flex;
    justify-content: space-between;
    .footer-left {
      display: flex;
      align-items: center;
      font-family: SF Pro Display;
      font-style: normal;
      font-weight: normal;
      font-size: 12px;
      line-height: 14px;
      color: rgba(0, 0, 0, 0.6);
      img {
        border-radius: 50%;
        width: 22px;
        height: 22px;
        margin-right: 4px;
      }
    }
    .like {
      display: flex;
      align-items: center;
      justify-content: center;
      font-family: SF Pro Display;
      font-style: normal;
      font-weight: normal;
      font-size: 12px;
      line-height: 14px;
      color: rgba(0, 0, 0, 0.4);
      img {
        margin-right: 4px;
      }
    }
  }
}
.cell-item {
  width: 100%;
  margin-bottom: 6px;
  background: #ffffff;
  border: 2px solid #f0f0f0;
  border-radius: 6px;
  overflow: hidden;
  box-sizing: border-box;
  img {
    width: 100%;
    height: auto;
    display: block;
  }
}
</style>
